/*
 * Copyright(c) 2018 优居科技 All rights reserved.
 * distributed with this file and available online at
 * http://www.yjyz.com/
 */
package com.zb.api.business.service.impl;


import com.zb.api.business.dto.CityDto;
import com.zb.api.business.dto.PosMerchantDto;
import com.zb.api.business.dto.UserDesignerDto;
import com.zb.api.business.entity.PosMerchantEntity;
import com.zb.api.business.entity.UserEntity;
import com.zb.api.business.mapper.PosMerchantMapper;
import com.zb.api.business.mapper.UserMapper;
import com.zb.api.business.service.IPosMerchantService;
import com.zb.api.common.econts.MerchantTypeEnum;
import com.zb.api.common.models.CityVo;
import com.zb.api.common.models.ComboBoxVO;
import com.zb.api.common.models.Page;
import com.zb.api.utils.UserThreadLocal;
import com.zb.api.utils.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.springframework.beans.BeanUtils.*;
import static org.springframework.beans.BeanUtils.copyProperties;


/**
* 商户表
 *
 * @author cwp
 * @version v1.0
 * @CreationTime: - 2022-02-08 14:19:30
 * @Description:
 */
@Service
public class PosMerchantServiceImpl implements IPosMerchantService {

    @Autowired
    private PosMerchantMapper posMerchantMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<PosMerchantDto> getPosMerchantList(int pageNo, int pageSize, PosMerchantDto posMerchantDto){
        /*PageHelper.startPage(pageNo, pageSize);
        PosMerchantEntity posMerchantEntity = BeansUtils.copyProperties(posMerchantDto, PosMerchantEntity.class);
        return BeanUtils.toPage(posMerchantMapper.getList(posMerchantEntity), PosMerchantDto.class);*/
        return null;
    }

    @Override
    public PosMerchantDto get(String id) {
        PosMerchantDto posMerchantDto = new PosMerchantDto();
        PosMerchantEntity entity = posMerchantMapper.get(id);
        if (entity!=null){
            BeanUtils.copyProperties(posMerchantMapper.get(id), posMerchantDto);
        }
        if (posMerchantDto.getType()!=null){
            posMerchantDto.setTypeName(MerchantTypeEnum.valueOf(posMerchantDto.getType()).getDesc());
        }
        return posMerchantDto;
    }

    @Override
    public int update(PosMerchantDto posMerchant){
        PosMerchantEntity entity = new PosMerchantEntity();
        copyProperties(posMerchant, entity);
        return posMerchantMapper.update(entity);
    }

    @Override
    public int insert(PosMerchantDto posMerchant){
        PosMerchantEntity entity = new PosMerchantEntity();
        copyProperties(posMerchant, entity);
        return posMerchantMapper.insert(entity);
    }

    @Override
    public int delete(PosMerchantDto posMerchant){
        PosMerchantEntity entity = new PosMerchantEntity();
        copyProperties(posMerchant, entity);
        return posMerchantMapper.delete(entity);
    }

    @Override
    public Integer registerMerchant(PosMerchantDto posMerchantDto) throws IllegalAccessException {
        if (posMerchantDto.getUserId()==null){
            throw new IllegalAccessException("用户id为空");
        }
        PosMerchantEntity entity = new PosMerchantEntity();
        copyProperties(posMerchantDto, entity);
        entity.setCreateTm(new Date().getTime());
        UserEntity user =  UserThreadLocal.get();
        entity.setUserId(user.getId());
        posMerchantMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public List<ComboBoxVO> getCityList(CityVo cityVo) {
        List<ComboBoxVO> comboBoxVOS = new ArrayList<>();
        List<CityVo> cityDtos = posMerchantMapper.getCityList(cityVo);
        if (!CollectionUtils.isEmpty(cityDtos)){
            cityDtos.forEach(f ->{
                ComboBoxVO comboBoxVO = new ComboBoxVO();
                comboBoxVO.setContent(f.getCityname());
                comboBoxVO.setValue(f.getId().toString());
                comboBoxVOS.add(comboBoxVO);
            });
        }
        return comboBoxVOS;
    }

    @Override
    public Integer addDesigner(UserDesignerDto userDesignerDto) {
        userDesignerDto.setCreateTm(new Date().getTime());
        posMerchantMapper.addDesigner(userDesignerDto);
        return userDesignerDto.getId();
    }

    @Override
    public Integer delDesigner(String id) {
        UserDesignerDto userDesignerDto = new UserDesignerDto();
        userDesignerDto.setId(Integer.valueOf(id));
        posMerchantMapper.delDesigner(userDesignerDto.getId());
        return 1;
    }

    @Override
    public List<UserDesignerDto> getDesignerList(UserDesignerDto userDesignerDto) {
        List<UserDesignerDto> list = posMerchantMapper.getDesignerList(userDesignerDto);
        return list;
    }
}
